package org.jeecg.modules.demo.cashier.service.impl;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.demo.basemembervipcoupon.entity.BaseMemberVipCoupon;
import org.jeecg.modules.demo.basemembervipcoupon.service.IBaseMemberVipCouponService;
import org.jeecg.modules.demo.cashier.mapper.CashierMapper;
import org.jeecg.modules.demo.cashier.service.CashierService;
import org.jeecg.modules.demo.commoditymanagement.entity.CommodityManagement;
import org.jeecg.modules.demo.commoditymanagement.service.ICommodityManagementService;
import org.jeecg.modules.demo.projectmanagement.entity.ProjectManagement;
import org.jeecg.modules.demo.projectmanagement.service.IProjectManagementService;
import org.jeecg.modules.util.StringUtils;
import org.jeecg.modules.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CashierServiceImpl implements CashierService {

    @Autowired
    private CashierMapper cashierMapper;
    @Autowired
    private ICommodityManagementService commodityManagementService;
    @Autowired
    private IProjectManagementService projectManagementService;
    @Autowired
    private IBaseMemberVipCouponService baseMemberVipCouponService;

    @Override
    public List<Map<String, Object>> searchMember(Map<String, Object> map) {
        return cashierMapper.searchMember(map);
    }

    @Override
    public Map<String, Object> getMember(Map<String, Object> map) {
        return cashierMapper.getMember(map);
    }

    @Override
    public List<Map<String, Object>> listCoupon1(Map<String, Object> map) {

        String storeId = map.get("storeId") == null ? null : String.valueOf(map.get("storeId"));
        String memberId = map.get("memberId") == null ? null : String.valueOf(map.get("memberId"));
        String totalCanUseDiscountAmountStr = map.get("totalCanUseDiscountAmount") == null ? null : String.valueOf(map.get("totalCanUseDiscountAmount"));
        BigDecimal totalCanUseDiscountAmount = new BigDecimal(totalCanUseDiscountAmountStr);

        List<BaseMemberVipCoupon> list = listCouponInTime(storeId, memberId);
        return list.stream().filter(o -> {
            // 时间早于起始时间，不能用
            if ("0".equals(o.getFullSelect())) {
                // x年x月-x年x月
                Date startDate = o.getFullDate();
                Date now = new Date();
                if (now.before(startDate)) {
                    return false;
                }
            }
            // type = 1 的 当前时间不可能比创建时间早
            // 满x减y的x
            BigDecimal amountLimit = new BigDecimal(o.getFullNum());
            return totalCanUseDiscountAmount.compareTo(amountLimit) >= 0;
        }).map(ToolsUtils::objectToMap).collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> listCoupon2(Map<String, Object> map) {

        String storeId = map.get("storeId") == null ? null : String.valueOf(map.get("storeId"));
        String memberId = map.get("memberId") == null ? null : String.valueOf(map.get("memberId"));
        String totalCanUseDiscountAmountStr = map.get("totalCanUseDiscountAmount") == null ? null : String.valueOf(map.get("totalCanUseDiscountAmount"));
        BigDecimal totalCanUseDiscountAmount = new BigDecimal(totalCanUseDiscountAmountStr);

        List<BaseMemberVipCoupon> list = listCouponInTime(storeId, memberId);
        List<Map<String, Object>> result = list.stream().filter(o -> {
            // 时间早于起始时间，不能用
            if ("0".equals(o.getFullSelect())) {
                // x年x月-x年x月
                Date startDate = o.getFullDate();
                Date now = new Date();
                if (now.before(startDate)) {
                    return true;
                }
            }
            // type = 1 的 当前时间不可能比创建时间早
            // 满x减y的x
            BigDecimal amountLimit = new BigDecimal(o.getFullNum());
            return totalCanUseDiscountAmount.compareTo(amountLimit) < 0;
        }).map(o -> {
            Map<String, Object> m = ToolsUtils.objectToMap(o);
            m.put("disabled", true);
            if ("0".equals(o.getFullSelect())) {
                // x年x月-x年x月
                Date startDate = o.getFullDate();
                Date now = new Date();
                if (now.before(startDate)) {
                    m.put("reason", "未到使用时间");
                    return m;
                }
            }
            m.put("reason", "订单金额不足");
            return m;
        }).collect(Collectors.toList());

        return result;
    }

    /**
     * 未过期的优惠券
     */
    private List<BaseMemberVipCoupon> listCouponInTime(String storeId, String memberId) {

        List<BaseMemberVipCoupon> list = cashierMapper.listCoupon(storeId, memberId);
        return list.stream().filter(o -> {
            if ("0".equals(o.getFullSelect())) {
                // x年x月-x年x月
                Date startDate = o.getFullDate();
                Date endDate = o.getFullNextDate();
                Date now = new Date();
                return now.before(endDate);
            }
            if ("1".equals(o.getFullSelect())) {
                // 领取x天以内
                Date createTime = o.getCreateTime();
                int day = Integer.parseInt(o.getManualDateNum());
                Calendar c = Calendar.getInstance();
                c.setTime(createTime);
                c.add(Calendar.DAY_OF_MONTH, day);
                Date endDate = c.getTime();
                Date now = new Date();
                return now.before(endDate);
            }
            // 永久有效
            return true;
        }).collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> listCoupon3(Map<String, Object> map) {

        String storeId = map.get("storeId") == null ? null : String.valueOf(map.get("storeId"));
        String memberId = map.get("memberId") == null ? null : String.valueOf(map.get("memberId"));

        List<BaseMemberVipCoupon> list = cashierMapper.listCoupon(storeId, memberId);
        return list.stream().filter(o -> {
            if ("0".equals(o.getFullSelect())) {
                // x年x月-x年x月
                Date endDate = o.getFullNextDate();
                Date now = new Date();
                return now.after(endDate);
            }
            if ("1".equals(o.getFullSelect())) {
                // 领取x天以内
                Date createTime = o.getCreateTime();
                int day = Integer.parseInt(o.getManualDateNum());
                Calendar c = Calendar.getInstance();
                c.setTime(createTime);
                c.add(Calendar.DAY_OF_MONTH, day);
                Date endDate = c.getTime();
                Date now = new Date();
                return now.after(endDate);
            }
            // 永久有效
            return false;
        }).map(o -> {
            Map<String, Object> m = ToolsUtils.objectToMap(o);
            m.put("disabled", true);
            m.put("reason", "已过期");
            return m;
        }).collect(Collectors.toList());
    }

    @Override
    public Map<String, Object> compute(Map<String, Object> map) {

        String storeId = map.get("storeId") == null ? null : String.valueOf(map.get("storeId"));
        String memberId = map.get("memberId") == null ? null : String.valueOf(map.get("memberId"));
        String useMemberDiscountStr = map.get("useMemberDiscount") == null ? null : String.valueOf(map.get("useMemberDiscount"));
        boolean useMemberDiscount = "1".equals(useMemberDiscountStr);
        String memberDiscountStr = map.get("memberDiscount") == null ? null : String.valueOf(map.get("memberDiscount"));
        BigDecimal memberDiscount = memberDiscountStr == null ? BigDecimal.TEN : new BigDecimal(memberDiscountStr);
        List<Map<String, Object>> commodityList = (List<Map<String, Object>>) map.get("commodityList");
        List<Map<String, Object>> couponList = (List<Map<String, Object>>) map.get("couponList");
        String reductionStr = map.get("reduction") == null ? "0" : String.valueOf(map.get("reduction"));
        log.info("kevin:commodityList数量：" + commodityList.size());
        log.info("kevin:commodityList：" + commodityList);
        log.info("kevin:couponList：" + couponList.size());
        // 手动优惠金额
        BigDecimal reduction = new BigDecimal(reductionStr);

        // 折扣
        BigDecimal realDiscount = BigDecimal.TEN;
        // 满减
        BigDecimal reduction2 = BigDecimal.ZERO;
        if (useMemberDiscount) {
            realDiscount = memberDiscount;
        } else {
            for (Map<String, Object> m : couponList) {
                String couponId = m.get("id") == null ? null : String.valueOf(m.get("id"));
                BaseMemberVipCoupon coupon = baseMemberVipCouponService.getById(couponId);
                if (coupon == null) {
                    continue;
                }
                // 礼品劵与金额无关，会员折扣、折扣劵、满减劵只有一个
                // 折扣劵
                if ("0".equals(coupon.getTicketType())) {
                    realDiscount = new BigDecimal(coupon.getFullNextNum());
                }
                // 满减劵
                if ("2".equals(coupon.getTicketType())) {
                    reduction2 = new BigDecimal(coupon.getFullNextNum());
                }
            }
        }

        // 订单金额
        BigDecimal totalAmount = BigDecimal.ZERO;
        // 参与优惠的金额
        BigDecimal totalCanUseDiscountAmount = BigDecimal.ZERO;
        // commodityList：*id(商品/项目id), *type(1.商品2.项目), *count(个数), free(是否赠送：1.是0.否), staffId(提成归属员工id)
        // 先计算总价
        for (Map<String, Object> m : commodityList) {
            JSONObject mJson = new JSONObject(m);
            log.info("kevin:m.toString:" + m.toString());
            log.info("kevin:m.get(id):" + m.get("id"));

            String id = m.get("id") == null ? null : String.valueOf(m.get("id"));
            String type = m.get("type") == null ? null : String.valueOf(m.get("type"));
            String countStr = m.get("count") == null ? "0" : String.valueOf(m.get("count"));
            BigDecimal count = new BigDecimal(countStr);
            String freeStr = m.get("free") == null ? null : String.valueOf(m.get("free"));
            boolean free = "1".equals(freeStr);
            BigDecimal price = new BigDecimal(mJson.getDouble("price"));  //获取前台传来的价格
            if ("1".equals(type)) {
                //商品
                CommodityManagement commodity = commodityManagementService.getById(id);
                log.info("kevin:商品id:" + id);
                log.info("kevin:商品实体:" + commodity);
                m.put("name", commodity.getTradeName());
                m.put("pic", commodity.getProductPic());
                m.put("specification", commodity.getSpecification());
                BigDecimal totalPrice = free ? BigDecimal.ZERO : price.multiply(count);
                totalAmount = totalAmount.add(totalPrice);
                if ("是".equals(commodity.getDiscount())) {
                    totalCanUseDiscountAmount = totalCanUseDiscountAmount.add(totalPrice);
                }
                m.put("totalPrice", totalPrice);
            } else {
                //项目
                ProjectManagement project = projectManagementService.getById(id);
                m.put("name", project.getProjectName());
                m.put("pic", project.getProjectPic());
                BigDecimal totalPrice = free ? BigDecimal.ZERO : price.multiply(count);
                totalAmount = totalAmount.add(totalPrice);
                if ("1".equals(project.getIsDiscount())) {
                    totalCanUseDiscountAmount = totalCanUseDiscountAmount.add(totalPrice);
                }
                m.put("totalPrice", totalPrice);
            }
        }

        // 不参与优惠的金额
        BigDecimal totalNoUseDiscountAmount = totalAmount.subtract(totalCanUseDiscountAmount);

        // 总优惠金额
        BigDecimal totalDiscountAmount = totalCanUseDiscountAmount.multiply(BigDecimal.TEN.subtract(realDiscount)).divide(BigDecimal.TEN, 2, RoundingMode.HALF_UP).add(reduction2).add(reduction);
        // 参与优惠部分实际支付金额
        BigDecimal realCanUseDiscountAmount = totalCanUseDiscountAmount.subtract(totalDiscountAmount);
        // 若参与优惠部分实际支付金额小于0，则参与优惠部分实际支付金额设置为0
        if (realCanUseDiscountAmount.compareTo(BigDecimal.ZERO) < 0) {
            totalDiscountAmount = totalCanUseDiscountAmount;
            realCanUseDiscountAmount = BigDecimal.ZERO;
        }
        // 实际支付金额 = 不参与优惠的金额 + 参与优惠部分实际支付金额
        BigDecimal realAmount = totalNoUseDiscountAmount.add(realCanUseDiscountAmount);

        // 再计算各商品的优惠价格
        for (Map<String, Object> m : commodityList) {

            String id = m.get("id") == null ? null : String.valueOf(m.get("id"));
            String type = m.get("type") == null ? null : String.valueOf(m.get("type"));
            String countStr = m.get("count") == null ? "0" : String.valueOf(m.get("count"));
            BigDecimal count = new BigDecimal(countStr);
            String freeStr = m.get("free") == null ? null : String.valueOf(m.get("free"));
            boolean free = "1".equals(freeStr);
            String staffId = m.get("staffId") == null ? null : String.valueOf(m.get("staffId"));

            if (totalCanUseDiscountAmount.compareTo(BigDecimal.ZERO) == 0) {
                m.put("discountAmount", BigDecimal.ZERO);
            } else {
                if ("1".equals(type)) {
                    CommodityManagement commodity = commodityManagementService.getById(id);
                    if ("是".equals(commodity.getDiscount())) {
                        BigDecimal price = new BigDecimal(String.valueOf(commodity.getProductPrice()));
                        BigDecimal totalPrice = free ? BigDecimal.ZERO : price.multiply(count);
                        BigDecimal discountAmount = totalDiscountAmount.multiply(totalPrice).divide(totalCanUseDiscountAmount, 2, RoundingMode.HALF_UP);
                        m.put("discountAmount", discountAmount);
                    } else {
                        m.put("discountAmount", BigDecimal.ZERO);
                    }
                } else {
                    ProjectManagement project = projectManagementService.getById(id);
                    if ("1".equals(project.getIsDiscount())) {
                        BigDecimal price = new BigDecimal(String.valueOf(project.getProjectPrice()));
                        BigDecimal totalPrice = free ? BigDecimal.ZERO : price.multiply(count);
                        BigDecimal discountAmount = totalDiscountAmount.multiply(totalPrice).divide(totalCanUseDiscountAmount, 2, RoundingMode.HALF_UP);
                        m.put("discountAmount", discountAmount);
                    } else {
                        m.put("discountAmount", BigDecimal.ZERO);
                    }
                }
            }

            if ("1".equals(type)) {
                CommodityManagement commodity = commodityManagementService.getById(id);
                BigDecimal price = new BigDecimal(String.valueOf(commodity.getProductPrice()));
                BigDecimal totalPrice = free ? BigDecimal.ZERO : price.multiply(count);
                String totalPriceStr = m.get("totalPrice") == null ? "0" : String.valueOf(m.get("totalPrice"));
                BigDecimal totalPrice2 = new BigDecimal(totalPriceStr);
                if (totalPrice.subtract(totalPrice2).doubleValue() > 0) {
                    m.put("discountAmount", totalPrice.subtract(totalPrice2).doubleValue());
                } else {
                    m.put("discountAmount", BigDecimal.ZERO);
                }
            } else {
                ProjectManagement project = projectManagementService.getById(id);

                BigDecimal price = new BigDecimal(String.valueOf(project.getProjectPrice()));
                BigDecimal totalPrice = free ? BigDecimal.ZERO : price.multiply(count);
                String totalPriceStr = m.get("totalPrice") == null ? "0" : String.valueOf(m.get("totalPrice"));
                BigDecimal totalPrice2 = new BigDecimal(totalPriceStr);
                if (totalPrice.subtract(totalPrice2).doubleValue() > 0) {
                    m.put("discountAmount", totalPrice.subtract(totalPrice2).doubleValue());
                } else {
                    m.put("discountAmount", BigDecimal.ZERO);
                }
            }
        }

        Map<String, Object> result = new HashMap<>();
        result.put("totalAmount", totalAmount);
        result.put("totalCanUseDiscountAmount", totalCanUseDiscountAmount);
        result.put("realAmount", realAmount);
        result.put("totalDiscountAmount", totalDiscountAmount);
        result.put("commodityList", commodityList);
        return result;
    }





    @Override
    public Map<String, Object> compute2(Map<String, Object> map) {

        String useMemberDiscountStr = map.get("useMemberDiscount") == null ? null : String.valueOf(map.get("useMemberDiscount"));
        boolean useMemberDiscount = "1".equals(useMemberDiscountStr);
        String memberDiscountStr = map.get("memberDiscount") == null ? null : String.valueOf(map.get("memberDiscount"));
        BigDecimal memberDiscount = memberDiscountStr == null ? BigDecimal.TEN : new BigDecimal(memberDiscountStr);
        List<Map<String, Object>> commodityList = (List<Map<String, Object>>) map.get("commodityList");
        List<Map<String, Object>> couponList = (List<Map<String, Object>>) map.get("couponList");
        String reductionStr = map.get("reduction") == null ? "0" : String.valueOf(map.get("reduction"));
        log.info("kevin:commodityList数量：" + commodityList.size());
        log.info("kevin:commodityList：" + commodityList);
        log.info("kevin:couponList：" + couponList.size());
        // 手动优惠金额
        BigDecimal reduction = new BigDecimal(reductionStr);

        // 折扣
        BigDecimal realDiscount = BigDecimal.TEN;
        // 满减
        BigDecimal reduction2 = BigDecimal.ZERO;
        if (useMemberDiscount) {
            realDiscount = memberDiscount;
        } else {
            for (Map<String, Object> m : couponList) {
                String couponId = m.get("id") == null ? null : String.valueOf(m.get("id"));
                BaseMemberVipCoupon coupon = baseMemberVipCouponService.getById(couponId);
                if (coupon == null) {
                    continue;
                }
                // 礼品劵与金额无关，会员折扣、折扣劵、满减劵只有一个
                // 折扣劵
                if ("0".equals(coupon.getTicketType())) {
                    realDiscount = new BigDecimal(coupon.getFullNextNum());
                }
                // 满减劵
                if ("2".equals(coupon.getTicketType())) {
                    reduction2 = new BigDecimal(coupon.getFullNextNum());
                }
            }
        }

        // 订单金额
        BigDecimal totalAmount = BigDecimal.ZERO;

        // 参与优惠的金额
        BigDecimal totalCanUseDiscountAmount = BigDecimal.ZERO;
        // commodityList：*id(商品/项目id), *type(1.商品2.项目), *count(个数), free(是否赠送：1.是0.否), staffId(提成归属员工id)
        // 先计算总价
        for (Map<String, Object> m : commodityList) {
            log.info("kevin:m.toString:" + m.toString());
            log.info("kevin:m.get(id):" + m.get("id"));

            String id = m.get("id") == null ? null : String.valueOf(m.get("id"));
            String type = m.get("type") == null ? null : String.valueOf(m.get("type"));
            String countStr = m.get("count") == null ? "0" : String.valueOf(m.get("count"));
            BigDecimal count = new BigDecimal(countStr);
            String freeStr = m.get("free") == null ? null : String.valueOf(m.get("free"));
            boolean free = "1".equals(freeStr);
            String staffId = m.get("staffId") == null ? null : String.valueOf(m.get("staffId"));
          //  String strPrice = m.get("price") == null ? null : String.valueOf(m.get("price"));

            if ("1".equals(type)) {
                CommodityManagement commodity = commodityManagementService.getById(id);

                m.put("name", commodity.getTradeName());
                m.put("pic", commodity.getProductPic());
                m.put("specification", commodity.getSpecification());
                BigDecimal price = new BigDecimal(String.valueOf(commodity.getProductPrice()));
                //m.put("price", price);
                // BigDecimal price = new BigDecimal(strPrice);
                BigDecimal totalPrice = free ? BigDecimal.ZERO : price.multiply(count);
                totalAmount = totalAmount.add(totalPrice);

                if ("是".equals(commodity.getDiscount())) {
                    totalCanUseDiscountAmount = totalCanUseDiscountAmount.add(totalPrice);
                }
                m.put("totalPrice", totalPrice);
            } else {
                ProjectManagement project = projectManagementService.getById(id);
                m.put("name", project.getProjectName());
                m.put("pic", project.getProjectPic());
                BigDecimal price = new BigDecimal(String.valueOf(project.getProjectPrice()));
                BigDecimal totalPrice = free ? BigDecimal.ZERO : price.multiply(count);
                totalAmount = totalAmount.add(totalPrice);
                if ("1".equals(project.getIsDiscount())) {
                    totalCanUseDiscountAmount = totalCanUseDiscountAmount.add(totalPrice);
                }
                m.put("totalPrice", totalPrice);
            }
        }

        // 不参与优惠的金额
        BigDecimal totalNoUseDiscountAmount = totalAmount.subtract(totalCanUseDiscountAmount);

        // 总优惠金额
        BigDecimal totalDiscountAmount = totalCanUseDiscountAmount.multiply(BigDecimal.TEN.subtract(realDiscount)).divide(BigDecimal.TEN, 2, RoundingMode.HALF_UP).add(reduction2).add(reduction);

        // 参与优惠部分实际支付金额
        BigDecimal realCanUseDiscountAmount = totalCanUseDiscountAmount.subtract(totalDiscountAmount);
        // 若参与优惠部分实际支付金额小于0，则参与优惠部分实际支付金额设置为0
        if (realCanUseDiscountAmount.compareTo(BigDecimal.ZERO) < 0) {
            totalDiscountAmount = totalCanUseDiscountAmount;
            realCanUseDiscountAmount = BigDecimal.ZERO;
        }
        // 实际支付金额 = 不参与优惠的金额 + 参与优惠部分实际支付金额
        BigDecimal realAmount = totalNoUseDiscountAmount.add(realCanUseDiscountAmount);

        // 再计算各商品的优惠价格
        for (Map<String, Object> m : commodityList) {

            String id = m.get("id") == null ? null : String.valueOf(m.get("id"));
            String type = m.get("type") == null ? null : String.valueOf(m.get("type"));
            String countStr = m.get("count") == null ? "0" : String.valueOf(m.get("count"));
            BigDecimal count = new BigDecimal(countStr);
            String freeStr = m.get("free") == null ? null : String.valueOf(m.get("free"));
            boolean free = "1".equals(freeStr);
            String staffId = m.get("staffId") == null ? null : String.valueOf(m.get("staffId"));

            if (totalCanUseDiscountAmount.compareTo(BigDecimal.ZERO) == 0) {
                m.put("discountAmount", BigDecimal.ZERO);
            } else {
                if ("1".equals(type)) {
                    CommodityManagement commodity = commodityManagementService.getById(id);
                    if ("是".equals(commodity.getDiscount())) {
                        BigDecimal price = new BigDecimal(String.valueOf(commodity.getProductPrice()));

                        BigDecimal totalPrice = free ? BigDecimal.ZERO : price.multiply(count);

                        BigDecimal discountAmount = totalDiscountAmount.multiply(totalPrice).divide(totalCanUseDiscountAmount, 2, RoundingMode.HALF_UP);
                        m.put("discountAmount", discountAmount);
                    } else {
                        m.put("discountAmount", BigDecimal.ZERO);
                    }
                } else {
                    ProjectManagement project = projectManagementService.getById(id);
                    if ("1".equals(project.getIsDiscount())) {
                        BigDecimal price = new BigDecimal(String.valueOf(project.getProjectPrice()));
                        BigDecimal totalPrice = free ? BigDecimal.ZERO : price.multiply(count);
                        BigDecimal discountAmount = totalDiscountAmount.multiply(totalPrice).divide(totalCanUseDiscountAmount, 2, RoundingMode.HALF_UP);
                        m.put("discountAmount", discountAmount);
                    } else {
                        m.put("discountAmount", BigDecimal.ZERO);
                    }
                }
            }

            if ("1".equals(type)) {
                CommodityManagement commodity = commodityManagementService.getById(id);
                BigDecimal price = new BigDecimal(String.valueOf(commodity.getProductPrice()));
                BigDecimal totalPrice = free ? BigDecimal.ZERO : price.multiply(count);
                String totalPriceStr = m.get("totalPrice") == null ? "0" : String.valueOf(m.get("totalPrice"));
                BigDecimal totalPrice2 = new BigDecimal(totalPriceStr);
                if (totalPrice.subtract(totalPrice2).doubleValue() > 0) {
                    m.put("discountAmount", totalPrice.subtract(totalPrice2).doubleValue());
                } else {
                    m.put("discountAmount", BigDecimal.ZERO);
                }
            } else {
                ProjectManagement project = projectManagementService.getById(id);

                BigDecimal price = new BigDecimal(String.valueOf(project.getProjectPrice()));
                BigDecimal totalPrice = free ? BigDecimal.ZERO : price.multiply(count);
                String totalPriceStr = m.get("totalPrice") == null ? "0" : String.valueOf(m.get("totalPrice"));
                BigDecimal totalPrice2 = new BigDecimal(totalPriceStr);
                if (totalPrice.subtract(totalPrice2).doubleValue() > 0) {
                    m.put("discountAmount", totalPrice.subtract(totalPrice2).doubleValue());
                } else {
                    m.put("discountAmount", BigDecimal.ZERO);
                }
            }
        }

        Map<String, Object> result = new HashMap<>();
        result.put("totalAmount", totalAmount);//商品价格
        result.put("totalDiscountAmount", totalDiscountAmount);//优惠金额
        result.put("realAmount", realAmount.subtract(reduction));//合计
        result.put("totalCanUseDiscountAmount", totalCanUseDiscountAmount);//总优惠金额  用不到
        result.put("commodityList", commodityList);
        return result;
    }

    @Override
    public List<Map<String, Object>> listProjectShop(String projectId) {
        return cashierMapper.listProjectShop(projectId);
    }

    @Override
    public List<Map<String, Object>> listProjectServe(String projectId) {
        return cashierMapper.listProjectServe(projectId);
    }

    @Override
    public List<Map<String, Object>> listMemberProject(Map<String, Object> map) {
        return cashierMapper.listMemberProject(map);
    }
}
